<?php
class CommonTag extends Tag{
    /**
     * 标签声明
     * @var array
     */
    public $Tag = array(
        'navigation' => array('block' => 1, 'level' => 4),
        'arclist' => array('block' => 1, 'level' => 4),
        'tag' => array('block' => 1, 'level' => 4),
        'flink'=> array('block' => 1, 'level' => 4),
    );

    /**
     * 导航标签
     * @param $attr 属性
     * @param $content 内容
     * @param $hd HdView模型引擎对象
     */
    public function _navigation($attr, $content, &$hd)
    {
      //获得条数
      $rows=isset($attr['rows']) ? $attr['rows'] :1000;
	  //处理数据
	  $str=<<<str
<?php
    \$topCate=K('Category')->where('pid=0 AND is_show=1')->limit({$rows})->order('csort ASC')->all();
    foreach (\$topCate as \$filed):

	\$filed['url'] = U('List/index',array('cid'=>\$filed['cid']));
?>
{$content}
<?php
endforeach
?>
str;
   return $str;
    }
	

    /**
     * 文章标签
     * @param $attr 属性
     * @param $content 内容
     * @param $hd HdView模型引擎对象
     */
    public function _arclist($attr, $content, &$hd){
     //获得的条数
		$rows = isset($attr['rows']) ? $attr['rows'] : 1000;
		//文章的属性
		$arcAttr = isset($attr['attr']) ? $attr['attr'] : NULL;
		//文章排序
		$order = isset($attr['sort']) ? $attr['sort'] : 'sendtime DESC';
		
		//WHERE条件组合
		$where = 'is_recycle=0';
		//如果有传递“热门，置顶”
		if($arcAttr){
			$where .= " AND attr like '%{$arcAttr}%'";
		}
		//如果在列表页的时候，会有cid
		$cid = Q('get.cid',0,'intval');
		//获得当前分类的子集cid
		$cateModel = K('Category');
		$sonCids = $cateModel->getSonCid($cid,$cateModel->where("is_show=1")->all());
		//把自己压入
		$sonCids[] = $cid;
		if($cid){
			$where .= " AND category_cid IN (".implode(',', $sonCids).")";
		}
		
		//分页处理
		//每页显示多少条
		$pageRows = isset($attr['pageRows']) ? $attr['pageRows'] : 0;
		//显示多少个页码
		$pageNum = isset($attr['pageNum']) ? $attr['pageNum'] : 5;
		
		//如果是点击了标签之后，有get参数tid
		$tid = Q('get.tid',0,'intval');
		if($tid){
			$aids = K('ArticleTag')->where("tag_tid={$tid}")->getField('article_aid',true);
			if($aids){
				$where .= " AND aid in(" . implode(',', $aids) . ")";
			}else{
				$where = "false";
			}
			
		}
	  
	  
	  
	  //处理数据
	  $str=<<<str
<?php
	//如果有分页的话
	if($pageRows){
		\$total = M()->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->where("{$where}")->count();
		\$pageObj = new Page(\$total,{$pageRows},{$pageNum});
		\$arcData = M()->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->where("{$where}")->limit(\$pageObj->limit())->order("{$order}")->all();
		\$page = \$pageObj->show();
	}else{
		\$arcData = M()->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->where("{$where}")->limit("{$rows}")->order("{$order}")->all();
	}
    foreach (\$arcData as \$filed):
	

    \$filed['url']=U('Content/index',array('aid'=>\$filed['aid']));
   //组合分类的url
   \$field['curl'] = U('List/index',array('cid'=>\$field['cid']));
   
    //评论总数
    \$filed['total'] = M('comment')->where("article_aid=" . \$filed['aid'])->count();
    
?>
{$content}
<?php
endforeach
?>
str;
   return $str;
    }	
	
	/**
     * 标签云标签
     * @param $attr 属性
     * @param $content 内容
     * @param $hd HdView模型引擎对象
     */
    public function _tag($attr, $content, &$hd)
    {
      //获得条数
      $rows=isset($attr['rows']) ? $attr['rows'] :1000;
	  $aid=Q('get.aid',0,'intval');
	  //如果点击到内容页
		if($aid){
			//通过aid获得对应的标签tid
			$tids = K('ArticleTag')->where("article_aid={$aid}")->getField('tag_tid',true);
			if($tids){
				//组合where条件
				$where = "tid in(" . implode(',', $tids) . ")";
			}else{
				//一个标签也不让查出来，所以是where条件为false
				$where = 'false';
			}
			
		}
	  //处理数据
	  $str=<<<str
<?php
if({$rows}){
	\$tagData = K('Tag')->limit('{$rows}')->where('{$where}')->all();
}else{
	\$tagData = K('Tag')->where('{$where}')->all();
}
foreach (\$tagData as \$filed):
\$filed['url'] = U('List/index',array('tid'=>\$filed['tid']));
?>
{$content}
<?php
endforeach
?>
str;
   return $str;
    }



/**
     * 友情链接标签
     * @param $attr 属性
     * @param $content 内容
     * @param $hd HdView模型引擎对象
     */
    public function _flink($attr, $content, &$hd)
    {
      //获得条数
      $rows=isset($attr['rows']) ? $attr['rows'] :1000;
	  //处理数据
	  $str=<<<str
<?php
    \$flink=K('Flink')->where('is_show=1')->limit({$rows})->all();

    foreach (\$flink as \$filed):
?>
{$content}
<?php
endforeach
?>
str;
   return $str;
    }
	
	
	
	
	
}